package com.shijiu.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.common.Result;
import com.shijiu.dao.mapper.*;
import com.shijiu.pojo.*;
import com.shijiu.service.OrderService;
import com.shijiu.service.ShopOrderService;
import com.shijiu.utils.UserThreadLocal;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.ListUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @Author xlj
 * @Date Created in  2023/5/7 15:42
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("echarts")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class EchartsController {

   @Resource
   private OrderMapper orderMapper;

   @Resource
   private UserMapper userMapper;

   @Resource
   private CateMapper cateMapper;

   @Resource
   private ShopMapper shopMapper;

   @Resource
   private FoodMapper foodMapper;

   @Resource
   private OrderFoodMapper orderFoodMapper;

   @Resource
   private ShopOrderMapper shopOrderMapper;
   @Resource
   private ShopOrderService shopOrderService;

   @ApiOperation("查询每季度的订单数量")
   @PostMapping("pie")
   public Result auditStatus(@RequestBody User user){
      List<Order> newArrayList = CollUtil.newArrayList();
      if ("admin".equals(user.getUsername())) {
         //admin 查所有的
         LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
         List<Order> orderList = orderMapper.selectList(wrapper);
         //Collections.copy(newArrayList, orderList);
         newArrayList = BeanUtil.copyToList(orderList, Order.class);
      } else {
         Integer shopId = getInteger(user);
         System.out.println(shopId);
         if (shopId == null) {
            return Result.fail(500,"用户信息有误！！！");
         }
         // 只查对应的店家的信息
         LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(Order::getShopId,shopId);
         List<Order> orderList = orderMapper.selectList(wrapper);
         newArrayList = BeanUtil.copyToList(orderList, Order.class);
      }
      //第一季度
      int q1 = 0 ;
      //第二季度
      int q2 = 0 ;
      //第三季度
      int q3 = 0 ;
      //第四季度
      int q4 = 0 ;
      for (Order order : newArrayList) {
         String orderCreatedTime = order.getCreated();
         DateTime time = DateUtil.parse(orderCreatedTime);
         Quarter quarter = DateUtil.quarterEnum(time);
         switch (quarter) {
            case Q1: q1 += 1;break;
            case Q2: q2 += 1;break;
            case Q3: q3 += 1;break;
            case Q4: q4 += 1;break;
            default: break;
         }
      }
      return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
   }
   @ApiOperation("查询")
   @PostMapping("bing")
   public Result bingResult() {
      return shopOrderService.selectALL();
   }
   @ApiOperation("查询")
   @PostMapping("zhexian")
   public Result cateResult(@RequestBody User user) {
      Integer shopId = getInteger(user);
      if (shopId == null) {
         return Result.fail(500,"用户信息有误！！！");
      }
      LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(Category::getShopId,shopId);
      //查出来每个商家下面所有的产品种类名称
      List<Category> categoryList = cateMapper.selectList(wrapper);
      List<String> categoryNameList = categoryList.stream()
              .map(Category::getName)
              .collect(Collectors.toList());
      //商品名称的
      List<Integer> categoryIdList = categoryList.stream().map(Category::getId).collect(Collectors.toList());
      Dict dict = Dict.create();
      //dict.set("name",categoryNameList);
      //查询每个产品的销售数量
      for (Integer categoryId: categoryIdList) {
         //System.out.println(categoryId);
         LambdaQueryWrapper<Food> wrapper1 = new LambdaQueryWrapper<>();
         wrapper1.eq(Food::getCategoryId,categoryId);
         Integer count = foodMapper.selectCount(wrapper1);
         System.out.println(count);
         // 查询完毕后，遍历每个类别名称，将数量赋值为查询到的结果
         for (int i = 0; i < categoryNameList.size(); i++) {
            String name = categoryNameList.get(i);
            if (categoryId.equals(categoryIdList.get(i))) {
               dict.put(name, count);
               break;
            }
         }
      }
      //HashMap 的扩展Dict继承HashMap，其key为String类型，value为Object类型，通过实现BasicTypeGetter接口提供针对不同类型的get方法，同时提供针对Bean的转换方法，大大提高Map的灵活性。
      return Result.success(dict);
   }
   private Integer getInteger(User user) {
      String username = user.getUsername();
      System.out.println(username);
      LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(User::getUsername,username);
      User selectedOne = userMapper.selectOne(queryWrapper);
      Integer shopId = selectedOne.getId();
      LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(Shop::getUserId,shopId);
      Shop shop = shopMapper.selectOne(wrapper);
      Integer shopId1 = shop.getId();
      return shopId1;
   }

}
